using System;
using System.Collections.Generic;
using System.Security.Principal;
using System.Text;
using Castle.ActiveRecord;
using NHibernate.Expression;

namespace PlanningPoker.Model
{
    
    [ActiveRecord(Table=CardPlayer.tablename)]
    public class CardPlayer : ActiveRecordBase<CardPlayer>, System.Security.Principal.IPrincipal
    {
        public const string tablename = "CardPlayers";
        public const string CardTableIdColumn = "CurrentTableId";
        private string _firstname;
        private string[] roles;
        private IIdentity identity;

        public CardPlayer()
        {
            
        }

        public CardPlayer(String name, String[] roles)
        {
            identity = new GenericIdentity(name, "Custom MonoRail authentication");
            this.roles = roles;
        }

        [Property]
        public string FirstName
        {
            get { return _firstname; }
            set { _firstname = value; }
        }
        private string _lastname;

        [Property]
        public string LastName
        {
            get { return _lastname; }
            set { _lastname = value; }
        }
        private string _nickname;

        [Property]
        public string NickName
        {
            get { return _nickname; }
            set { _nickname = value; }
        }

        private string _username;

        [Property]
        public string Username
        {
            get { return _username; }
            set { _username = value; }
        }

        private string _password;

        [Property]
        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        private CardTable _currenttable;

        
        [BelongsTo(CardPlayer.CardTableIdColumn)]
        public CardTable CurrentTable
        {
            get { return _currenttable; }
            set { _currenttable = value; }

        }
        private int _id;
        
        [PrimaryKey]
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

    //IPrincipal Implementation
        

        public bool IsInRole(string role)
        {
            return Array.IndexOf(roles, role) >= 0;
        }

        public IIdentity Identity
        {
            get { return identity; }
        }

        public static CardPlayer FindCardPlayer(string UserName, 
												string Password){

            CardPlayer user = FindOne(NHibernate.Expression.Expression.And(
						Expression.Eq("Username", UserName),
						Expression.Eq("Password", Password)));
			return user;
        }

    }
}
